package com.shop.prj.dao.Impl;

import com.shop.prj.bean.BookStyle;
import com.shop.prj.bean.Manager;
import com.shop.prj.dao.ManagerDao;
import com.shop.prj.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ManagerDaoImpl implements ManagerDao {
    @Override
    public boolean login(Manager manager) {
        String sql = "SELECT * FROM `Manager` WHERE `manager_name` = '" + manager.getManagerName() + "' AND `manager_password` = '"
                + manager.getManagerPassword() + "'";
     
        Connection connection = JDBCUtils.getConnection();
       
        Statement statement = null;
       
        ResultSet resultSet = null;
        Manager manager2 = null;
        try {
         
            statement = connection.createStatement();
            
            resultSet = statement.executeQuery(sql);
       
            if (resultSet.next()) {
                manager2 = new Manager();
                manager2.setManagerId(resultSet.getInt(1));
                manager2.setManagerName(resultSet.getString(2));
                manager2.setManagerPassword(resultSet.getString(3)) ;
                manager2.setManagerCreateTime(resultSet.getDate(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return (manager2 != null);
    }

    @Override
    public int insert(Manager manager) {
        String sql="INSERT INTO `manager`(`manager_name`,`manager_password`,`manager_create_time`)VALUES(?,?,?)";
        int result=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        connection= JDBCUtils.getConnection();
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, manager.getManagerName());
            preparedStatement.setString(2, manager.getManagerPassword());
            preparedStatement.setDate(3,manager.getManagerCreateTime());
            result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return result;
    }

    @Override
    public int update(Manager manager) {
        String sql="UPDATE `manager` SET `manager_name`=?,`manager_password`=? ,`manager_create_time`=?WHERE `manager_id`=?";
        int result=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        connection= JDBCUtils.getConnection();
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, manager.getManagerName());
            preparedStatement.setString(2, manager.getManagerPassword());
            preparedStatement.setDate(3,manager.getManagerCreateTime());
            preparedStatement.setInt(4, manager.getManagerId());
            result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return result;
    }

    @Override
    public int delete(Integer managerId) {
        String sql = "DELETE FROM `manager` WHERE `manager_id` = ?";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1,managerId);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public Manager queryByManageId(Integer managerId) {
        String sql = "SELECT * FROM `manager` WHERE `manager_Id`=?";

        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;

        ResultSet resultSet = null;
       Manager manager = null;

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1,managerId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
            	manager=new Manager();
                manager.setManagerId(resultSet.getInt(1));
                manager.setManagerName(resultSet.getString(2));
                manager.setManagerPassword(resultSet.getString(3));
                manager.setManagerCreateTime(resultSet.getDate(4));



            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }


        return manager;
    }

    @Override
    public List<Manager> queryAll() {
        String sql = "SELECT * FROM `bookStyle`";

        Connection connection = JDBCUtils.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        List<Manager> list = new ArrayList<Manager>();
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {

               Manager manager=new Manager();
                manager.setManagerId(resultSet.getInt(1));
                manager.setManagerName(resultSet.getString(2));
                manager.setManagerPassword(resultSet.getString(3));
                manager.setManagerCreateTime(resultSet.getDate(4));
                list.add(manager);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public List<Manager> queryByManagerName(String name) {
        return null;
    }

	@Override
	public Manager queryByManage(String name) {
		// TODO Auto-generated method stub
		  String sql = "SELECT * FROM `manager` WHERE `manager_name`=?";

	        Connection connection = JDBCUtils.getConnection();
	        PreparedStatement preparedStatement = null;

	        ResultSet resultSet = null;
	       Manager manager = null;

	        try {
	            preparedStatement = connection.prepareStatement(sql);
	            preparedStatement.setString(1,name);
	            resultSet = preparedStatement.executeQuery();
	            if (resultSet.next()) {
	            	manager=new Manager();
	                manager.setManagerId(resultSet.getInt(1));
	                manager.setManagerName(resultSet.getString(2));
	                manager.setManagerPassword(resultSet.getString(3));
	                manager.setManagerCreateTime(resultSet.getDate(4));



	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                resultSet.close();
	                preparedStatement.close();
	                connection.close();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }

	        }


	        return manager;
	}
}
